package cn.bdqn.dao;

import cn.bdqn.entity.Student;

import java.sql.*;

public class StudentDao {
    //根据学生的名字找到对应的学号
    public String getNobyName(String name){
        //注册驱动
        String sno=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String connstr="jdbc:mysql:///myschool?characterEncoding=utf8";
            //创建连接对象
            Connection conn= DriverManager.getConnection(connstr,"root","root");
            //创建表达式对象
            Statement stmt = conn.createStatement();
            String sql="select studentNo from student where studentName='"+name+"'";
            //执行SQL语句，返回一个结果集对象
            ResultSet rs = stmt.executeQuery(sql);
            if (rs.next()){
                sno=rs.getString(1);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  sno;
    }


    //登录方法
    public Student login(String no, String pwd) {
        //注册驱动
        Student student= null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String connstr="jdbc:mysql:///myschool?characterEncoding=utf8";
            //创建连接对象
            Connection conn= DriverManager.getConnection(connstr,"root","root");
            //创建表达式对象
            Statement stmt = conn.createStatement();
            String sql="select * from student where studentNo='"+no+"' and loginPwd='"+pwd+"'";
            //执行SQL语句，返回一个结果集对象
            ResultSet rs = stmt.executeQuery(sql);
            student = null;
            if (rs.next()){
                student=new Student();
                student.setStudentNo(rs.getString(1));
                student.setLoginPwd(rs.getString(2));
                student.setStudentName(rs.getString(3));
                student.setSex(rs.getString(4));
                student.setGradeId(rs.getInt(5));
                student.setPhone(rs.getString(6));
                student.setAddress(rs.getString(7));
                student.setBornDate(rs.getDate(8));
                student.setEmail(rs.getString(9));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  student;
    }

}
